package org.cerberus.platform.internal.server.jetty;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cerberus.platform.internal.IObjectGraphTransaction;
import org.mortbay.jetty.handler.AbstractHandler;

public class JettyHandler extends AbstractHandler {

	private static final String CONTENT_TYPE = "application";
	
	private JettyServer server;
	
	public JettyHandler(JettyServer server) {
		this.server = server;
	}

	@Override
	public void handle(String target, HttpServletRequest request,
			HttpServletResponse response, int dispatch) throws IOException,
			ServletException {
	}
	
	void writeTransaction(HttpServletResponse response, IObjectGraphTransaction transaction) throws IOException {
		response.setContentType(CONTENT_TYPE);
		ServletOutputStream out = response.getOutputStream();
		writeHeader(out, transaction);
		
		out.flush();
		transaction.getAddClasses();
	}
	
	void writeHeader(OutputStream out, IObjectGraphTransaction transaction) throws IOException {
		out.write("<platformResponse>".getBytes());
		out.write(transaction.getAddClasses() != null ? transaction.getAddClasses().size() : 0);
		out.write(transaction.getRemoveClasses() != null ? transaction.getRemoveClasses().size() : 0);
		out.write(transaction.getAddObjects() != null ? transaction.getAddObjects().size() : 0);
		out.write(transaction.getRemoveObjects() != null ? transaction.getRemoveObjects().size() : 0);
		out.write(transaction.getObjectChanges() != null ? transaction.getObjectChanges().size() : 0);		
	}
	
	
	
}
